using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Collections.Specialized;
using System.Drawing;

namespace LogFileViewer
{
    public partial class MainWindow : Form
    {

        private ToolStripRenderMode renderMode;

        private void LoadWindowSettings()
        {
            listView.ListViewItemSorter = lvwColumnSorter;

            if (sets != null)
            {
                // TYPES
                if (sets.SelectedTypes == null || sets.SelectedTypes.Count == 0)
                {
                    sets.SelectedTypes = new StringCollection();
                    sets.SelectedTypes.Add(MessageTypes.ERROR.ToString());
                    sets.SelectedTypes.Add(MessageTypes.FATAL.ToString());
                    sets.Save();
                }

                // REFRESHING HISTORY
                RefreshHistoryEntries();

                // CHANGE BUTTON STATES
                changeButtonStates();

                // SETTING WINDOW POSITION & SIZE
                if (sets.MainWindowPosX == 0 || sets.MainWindowPosY == 0)
                {
                    sets.MainWindowPosX = this.Bounds.X;
                    sets.MainWindowPosY = this.Bounds.Y;
                }
                this.Bounds = new Rectangle(sets.MainWindowPosX, sets.MainWindowPosY, sets.MainWindowWidth, sets.MainWindowHeight);

                // SETTING CONSOLE FONT
                if (sets.ConsoleFont != null)
                {
                    listView.Font = sets.ConsoleFont;
                }

                // SETTING CONSOLE WIDTHS
                if (sets.ConsoleWidths != null)
                {
                    for (int i = 0; i < sets.ConsoleWidths.Length; i++)
                    {
                        listView.Columns[i].Width = sets.ConsoleWidths[i];
                    }
                }
                // TODO: save and load sort order


                // SETTING LAST LOADED FILE
                setLastLoadedFileDialog();

                if ((sets.HandleReadMethod == (int)DataAccess.ReaderHandling.ActiveRows) && this.dataAccess != null && this.dataAccess.LineIndexes != null)
                {
                    // Iterator settings for active row reload operations
                    setListViewVirtualMode();

                    // Update statusbar "All Entries"
                    UpdateAllEntriesInfoStatusbar();
                }

                // CREATE LISTVIEW CONTEXT MENU ITEMS
                if (sets.ColumnsVisible == null)
                {
                    sets.ColumnsVisible = new Boolean[listView.Columns.Count];
                }
                CreateListViewContextMenuItems();

                // Setting style of controls
                if (sets.CurrentWindowStyle == 0)
                {
                    renderMode = ToolStripRenderMode.System;
                }
                else
                {
                    renderMode = ToolStripRenderMode.Professional;
                }
                contextMenuListView.RenderMode = this.renderMode;
                menuStripMainWindow.RenderMode = this.renderMode;
            }
        }


        private void SaveWindowSettings()
        {
            // Setting window size & position
            sets.MainWindowWidth = this.Width;
            sets.MainWindowHeight = this.Height;
            sets.MainWindowPosX = this.Bounds.X;
            sets.MainWindowPosY = this.Bounds.Y;

            // Setting console widths
            int columns = listView.Columns.Count;
            int[] widths = new int[columns];
            for (int i = 0; i < columns; i++)
            {
                widths[i] = listView.Columns[i].Width;
            }
            sets.ConsoleWidths = widths;

            // Saving everything
            sets.Save();
        }
    }
}
